package com.java3_9;


public class Account {
    private final String id;
    private int balance;

    public Account(String id, int balance) {
        this.id = id;
        this.balance = balance;
    }

    public String getId() {
        return id;
    }

    public int getBalance() {
        return balance;
    }

    @Override
    public String toString() {
        return "账户； " + id + "余额； " + balance;
    }

    public void save(int money) {
        synchronized (Account.class) {
            Account.class.notify();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            balance += money;
            System.out.println(Thread.currentThread().getName() + "向卡里存入了" + money + "元钱" + toString());
        }
    }

    public void withdraw(int money) {
        synchronized (Account.class) {
            if (balance > money) {
                balance -= money;
                System.out.println(Thread.currentThread().getName() + "向卡里取走了了" + money + "元钱" + toString());
            } else {
                try {
                    System.out.println("卡里的余额不足，需要充钱！");
                    Account.class.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }
}
